<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增角色 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'carbon:network-interface',
                tooltip: '接口授权',
                onClick: handleInterface.bind(null, record),
              },
              {
                icon: 'material-symbols:buttons-alt',
                tooltip: '按钮授权',
                onClick: handleButton.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <InterfaceDrawer @register="interfaceDrawer" @success="handleSuccess" />
    <ButtonDrawer @register="buttonDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getRoleList, deleteRoleIds } from '@/api/system/role';

  import { useDrawer } from '@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import InterfaceDrawer from './InterfaceDrawer.vue';
  import ButtonDrawer from './ButtonDrawer.vue';

  import { columns, searchFormSchema } from './role.data';

  import { useMessage } from '@/hooks/web/useMessage';
  import { useI18n } from '@/hooks/web/useI18n';

  defineOptions({ name: 'RoleManagement' });
  const { createMessage } = useMessage();

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [interfaceDrawer, { openDrawer: openDrawer2 }] = useDrawer();
  const [buttonDrawer, { openDrawer: openDrawer3 }] = useDrawer();

  const [registerTable, { reload }] = useTable({
    title: '角色管理',
    api: getRoleList,
    columns,
    formConfig: {
      labelWidth: 100,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    // showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    pagination: false,
    size: 'small',
    actionColumn: {
      width: 180,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleInterface(record: Recordable) {
    openDrawer2(true, {
      record,
      isUpdate: true,
    });
  }

  function handleButton(record: Recordable) {
    openDrawer3(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    const { t } = useI18n();
    const id: any = record?.id;
    const { code, message } = await deleteRoleIds(id);
    if (code == 0) {
      createMessage.success(t('sys.api.deleteSuccess'));
      reload();
    } else {
      createMessage.error(message);
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
